package com.example.demo1.service;


import com.alibaba.nacos.api.NacosFactory;
import com.alibaba.nacos.api.PropertyKeyConst;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.exception.NacosException;
import com.example.demo1.pojo.StudentCollectionPojo;
import com.example.demo1.pojo.StudentPojo;
import com.google.gson.Gson;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Properties;

@Service
public class NacosConfigService {

    public StudentCollectionPojo getStudentCollection() throws NacosException {


        // 获取配置(客户端方法)
        String serverAddr = "39.108.139.168:8848";
        String dataId = "app-students";
        String group = "biz01";
        Properties properties = new Properties();
        properties.put(PropertyKeyConst.SERVER_ADDR, serverAddr);
        ConfigService configService = NacosFactory.createConfigService(properties);

        String content = configService.getConfig(dataId, group, 5000);
        System.out.println(content);


        Gson gson = new Gson();
        StudentCollectionPojo studentCollectionPojo = gson.fromJson(content, StudentCollectionPojo.class);
        System.out.println("获取学生数量:" + studentCollectionPojo.getCount());
        return studentCollectionPojo;
    }


}
